package gupao.concurrency.threads;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

public class AtomicStampedReferenceSample {
    static class StatusSample {
        final static int STATUS_1 = 1;
        final static int STATUS_2 = 2;
        final static int STATUS_3 = 3;

        AtomicStampedReference<Integer> statusRef =
                new AtomicStampedReference<>(STATUS_1, 1);

        public void setStatus(int newStatus) {
            Integer currentStatus;
            int currentStamp;
            for ( ; ; ) {
                currentStatus = statusRef.getReference();
                currentStamp = statusRef.getStamp();
                int newStamp = currentStamp + 1;
                if (statusRef.compareAndSet(
                        currentStatus,
                        newStatus,
                        currentStamp,
                        newStamp)) {
                    break;
                }
            }
        }
    }
}
